﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using PassBag.Data;

namespace PassBag.Logic.Settings {

    /// <summary>
    /// Help to save the settings to a XML file
    /// </summary>
    public sealed class XmlSettingsManager {
        private static XmlSettings _settings = null;
        private const string SETTINGS_DIRECTORY = "Settings";
        private const string SETTINGS_FILENAME = "settings.xml";

        /// <summary>
        /// Gets the settings.
        /// </summary>
        /// <value>The settings.</value>
        public static XmlSettings Settings {
            get {
                InitSettings ();
                return _settings;
            }
        }

        /// <summary>
        /// Gets the name of the setting file.
        /// </summary>
        /// <value>The name of the setting full file.</value>
        public static string SettingFullFileName {
            get {
                // 200.01.23 - BUG0001 - Add full path to setting file - see
                return PassBag.Utils.ApplicationPath.Directory + System.IO.Path.DirectorySeparatorChar + SETTINGS_DIRECTORY + System.IO.Path.DirectorySeparatorChar + SETTINGS_FILENAME;
            }
        }

        /// <summary>
        /// Gets the setting directory.
        /// </summary>
        /// <value>The setting directory.</value>
        public static string SettingDirectory {
            get {
                return SETTINGS_DIRECTORY;
            }
        }

        /// <summary>
        /// Gets the name of the setting file.
        /// </summary>
        /// <value>The name of the setting file.</value>
        public static string SettingFileName {
            get {
                return SETTINGS_FILENAME;
            }
        }

        private static void InitSettings () {
            if (_settings == null) {
                _settings = new XmlSettings ();
                Load ();

            }
            if (!System.IO.File.Exists (XmlSettingsManager.SettingFullFileName)) {
                if (!System.IO.Directory.Exists (SETTINGS_DIRECTORY)) {
                    System.IO.Directory.CreateDirectory (SETTINGS_DIRECTORY);
                    Save ();
                }
            }
        }

        /// <summary>
        /// Reloads from file.
        /// </summary>
        /// <returns><c>true</c> if [SUCCESS]; otherwise, <c>false</c>.</returns>
        public static bool ReloadFromFile () {
            try {
                _settings = null;
                InitSettings ();
                return false;

            }
            catch {
                return true;
            }
        }

        /// <summary>
        /// Saves the settings to an xml file
        /// </summary>
        /// <returns><c>true</c> if [SUCCESS]; otherwise, <c>false</c>.</returns>
        public static bool Save () {
            InitSettings ();
            // debug
            PassBag.Utils.Logger.DebugWrite("save: " + PassBag.Utils.Logger.GetFullFileName(XmlSettingsManager.SettingFullFileName));
            return ObjectSerializer.SerializeObject<XmlSettings> (XmlSettingsManager.SettingFullFileName, _settings);
        }

        /// <summary>
        /// Loads the settings from an xml file
        /// </summary>
        /// <returns><c>true</c> if [SUCCESS]; otherwise, <c>false</c>.</returns>
        private static bool Load () {
            try {
                PassBag.Utils.Logger.DebugWrite("save: " + PassBag.Utils.Logger.GetFullFileName(XmlSettingsManager.SettingFullFileName));
                _settings = ObjectSerializer.DeserializeObject<XmlSettings> (XmlSettingsManager.SettingFullFileName);
                return true;
            }
            catch {
                return false;
            }
        }

    }
}
